package com.gzj.environment.module.collectData.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.gzj.environment.enums.code.impl.collectDataCode.CollectDataCode;
import com.gzj.environment.framework.exception.CustomerException;
import com.gzj.environment.model.collectData.criteria.QueryCriteria;
import com.gzj.environment.model.collectData.dto.AirCollectData;
import com.gzj.environment.module.collectData.context.ClientContext;
import com.gzj.environment.module.collectData.properties.NetworkProperties;
import com.gzj.environment.module.collectData.repository.AirCollectDataRepository;
import com.gzj.environment.module.collectData.service.ManagerService;
import com.gzj.environment.module.collectData.util.ChannelUtils;
import io.netty.channel.ChannelHandlerContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@EnableConfigurationProperties(NetworkProperties.class)
public class ManagerServiceImpl implements ManagerService {

    @Autowired
    private NetworkProperties networkProperties;

    /**
     * 根据条件查询客户端版本号
     *
     * @param criteria 查询条件
     * @return
     */
    @Override
    public Map<String, String> queryVersion(QueryCriteria criteria) {
        // 获取需要查询的客户端信道
        List<ChannelHandlerContext> list = ClientContext.getChannelByCriteria(criteria);
        return ClientContext.getClientVersion(list);
    }


    /**
     * 根据查询条件，对指定的信道发送获取版本的命令
     *
     * @param criteria 查询条件
     */
    @Override
    public void sendVersionCmd(QueryCriteria criteria) {
        // 获取需要查询的客户端信道
        List<ChannelHandlerContext> list = ClientContext.getChannelByCriteria(criteria);
        if (CollectionUtil.isEmpty(list)) {
            throw new CustomerException(CollectDataCode.HAS_NO_ONLINE_CLIENT);
        }
        // 逐一发送获取版本的命令
        for (ChannelHandlerContext ctx : list) {
            ChannelUtils.writeToClient(networkProperties.getVersionCommand(), ctx, "获取版本");
        }
    }

}
